package com.yjt.fresh.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiGetLastVersion;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanLastVersion;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 设置
 * Created by mcc on 2018/7/21.
 */

public class AtySetting extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rlAppAtySettingHelpCenter)
    RelativeLayout mRlHelpCenter;
    @BindView(R.id.rlAppAtySettingAboutUs)
    RelativeLayout mRlAboutUs;
    @BindView(R.id.rlAppAtySettingAdvice)
    RelativeLayout mRlAdvice;
    @BindView(R.id.rlAppAtySettingUpdate)
    RelativeLayout mRlUpdate;
    @BindView(R.id.btnAppAtySettingLogout)
    Button mBtnLogout;
    @BindView(R.id.tvAppAtySettingVersion)
    TextView mTvVersion;
    private int mVersionCode;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_setting;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mVersionCode = CommonsToolsHelper.getVersionCode(mContext);
        mTvVersion.setText("当前版本v"+ mVersionCode);
        if (PreferenceManager.getInstance().getIsLogin()){
            mBtnLogout.setVisibility(View.VISIBLE);
        }else {
            mBtnLogout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initEvent() {
        mRlHelpCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyHelpCenter.class);
                startActivity(intent);
            }
        });

        mRlAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyAboutUs.class);
                startActivity(intent);
            }
        });

        mRlAdvice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceManager.getInstance().getIsLogin()){
                    Intent intent = new Intent(mContext,AtyAdvice.class);
                    startActivity(intent);
                }else {
                    loginDialog();
                }
            }
        });

        mRlUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                getLastVersion();
                Beta.checkUpgrade();
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mBtnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logoutDialog();
            }
        });
    }

    private void loginDialog() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvContent.setText("登录才可以提交意见哦~亲");
        tvCancel.setText("取消");
        tvSure.setText("立即登录");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AtyLogin.class);
                startActivity(intent);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }

    private void logoutDialog() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvContent.setText("确认退出登录?");
        tvCancel.setText("取消");
        tvSure.setText("退出");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerAndSendBroadcast(MyConstants.LOGOUT);
                PreferenceManager.getInstance().setIsLogin(false);
                PreferenceManager.getInstance().clearUserInfo();
                Intent intent = new Intent(mContext,AtyLogin.class);
                startActivity(intent);
                finish();
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }
    HttpOnNextListener<BeanLastVersion> mListener = new HttpOnNextListener<BeanLastVersion>() {
        @Override
        public void onNext(BeanLastVersion beanLastVersion) {
            if (Double.valueOf(beanLastVersion.getVERSION_NO())>mVersionCode){
                //TODO apk更新
                CustomToast.showLong(beanLastVersion.getVERSION_DESCRIBE());
            }else {
                CustomToast.showLong("软件当前已为最新版本");
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void getLastVersion(){
        ApiGetLastVersion apiGetLastVersion = new ApiGetLastVersion(mListener,mContext,true,false,true);
        HttpManager.getInstance().doHttpDeal(apiGetLastVersion);
    }

}
